﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;

namespace YArchitech.LIB
{
	public static class Product
	{
		public static string InstallLocation
		{
			get
			{
				if (Product.installLocation == null)
				{
					Product.installLocation = Path.GetDirectoryName(Product.BinLocation);
				}
				return Product.installLocation;
			}
		}

		public static string BinLocation
		{
			get
			{
				if (string.IsNullOrEmpty(Product.binLocation))
				{
					Product.binLocation = Assembly.GetExecutingAssembly().Location;
					Product.binLocation = Path.GetDirectoryName(Product.binLocation);
				}
				return Product.binLocation;
			}
		}

		public static string DataLocation
		{
			get
			{
				return Path.Combine(Product.InstallLocation, "Data");
			}
		}

		public static string ResLocation
		{
			get
			{
				return Path.Combine(Product.InstallLocation, "Res");
			}
		}

		public static string FamilyLocation
		{
			get
			{
				return Path.Combine(Product.InstallLocation, "FamilyLib", "Family");
			}
		}

		public static string FamilyImageLocation
		{
			get
			{
				return Path.Combine(Product.InstallLocation, "FamilyLib", "FamilyImage");
			}
		}

		public static string DefaultConfigFile
		{
			get
			{
				return Product.defaultConfigFile;
			}
		}

		public static string ConfigFile
		{
			get
			{
				return Product.configFile;
			}
		}

		public static string ConfigFileName
		{
			get
			{
				return Product.configFile;
			}
		}

		public static string ProgramDataLocation
		{
			get
			{
				if (string.IsNullOrEmpty(Product.programDataLocation))
				{
					Product.programDataLocation = Environment.ExpandEnvironmentVariables(IniHelper.GetSetting("System", "ProgramDataLocation", Product.DefaultConfigFile, ""));
				}
				return Product.programDataLocation;
			}
		}

		public static string WorkingLocation
		{
			get
			{
				if (string.IsNullOrEmpty(Product.workingLocation))
				{
					Product.workingLocation = Environment.ExpandEnvironmentVariables(IniHelper.GetSetting("System", "WorkingLocation", Product.DefaultConfigFile, ""));
				}
				return Product.workingLocation;
			}
		}

		public static string UserDataLocation
		{
			get
			{
				return Path.Combine(Product.WorkingLocation, "UserData");
			}
		}

		public static string UserConfigFileName
		{
			get
			{
				if (string.IsNullOrEmpty(Product.userConfigFileName))
				{
					Product.userConfigFileName = IniHelper.GetSetting("Data", "UserConfigFileName", Product.DefaultConfigFile, "");
				}
				return Product.userConfigFileName;
			}
		}

		public static string UserConfigFile
		{
			get
			{
				if (string.IsNullOrEmpty(Product.m_userConfigFile))
				{
					Product.m_userConfigFile = Path.Combine(Product.UserDataLocation, Product.UserConfigFileName);
				}
				return Product.m_userConfigFile;
			}
		}

		public static string DataSourceFileName
		{
			get
			{
				if (string.IsNullOrEmpty(Product.m_DataSourceFilename))
				{
					Product.m_DataSourceFilename = IniHelper.GetSetting("Data", "DataSourceFileName", Product.DefaultConfigFile, "");
				}
				return Product.m_DataSourceFilename;
			}
		}

		public static string DataFamilySourceFileName
		{
			get
			{
				if (string.IsNullOrEmpty(Product.m_DataFamilySourceFilename))
				{
					Product.m_DataFamilySourceFilename = IniHelper.GetSetting("Data", "DataFamilySourceFileName", Product.DefaultConfigFile, "");
				}
				return Product.m_DataFamilySourceFilename;
			}
		}

		public static string UserDataSourceFile
		{
			get
			{
				if (string.IsNullOrEmpty(Product.m_userDataSourceFile))
				{
					Product.m_userDataSourceFile = Path.Combine(Product.UserDataLocation, Product.DataSourceFileName);
				}
				return Product.m_userDataSourceFile;
			}
		}

		public static string PipeStandardFileName
		{
			get
			{
				if (string.IsNullOrEmpty(Product.m_PipeStandardFileName))
				{
					Product.m_PipeStandardFileName = IniHelper.GetSetting("Data", "PipeStandardFileName", Product.DefaultConfigFile, "");
				}
				return Product.m_PipeStandardFileName;
			}
		}

		public static string UserPipeStandardFile
		{
			get
			{
				return Path.Combine(Product.UserDataLocation, Product.PipeStandardFileName);
			}
		}

		public static string SharedParametersFileName
		{
			get
			{
				return IniHelper.GetSetting("Data", "SharedParametersFileName", Product.DefaultConfigFile, "");
			}
		}

		public static string UserSharedParametersFile
		{
			get
			{
				if (string.IsNullOrEmpty(Product.m_userSharedParametersFile))
				{
					Product.m_userSharedParametersFile = Path.Combine(Product.UserDataLocation, Product.SharedParametersFileName);
				}
				return Product.m_userSharedParametersFile;
			}
		}

		public static bool Init()
		{
			if (!Directory.Exists(Product.WorkingLocation))
			{
				Directory.CreateDirectory(Product.WorkingLocation);
			}
			if (!Directory.Exists(Product.UserDataLocation))
			{
				Directory.CreateDirectory(Product.UserDataLocation);
			}
			if (!File.Exists(Path.Combine(Product.WorkingLocation, Product.DataFamilySourceFileName)))
			{
				File.Copy(Path.Combine(Product.DataLocation, Product.DataFamilySourceFileName), Path.Combine(Product.WorkingLocation, Product.DataFamilySourceFileName));
			}
			if (!File.Exists(Path.Combine(Product.UserDataLocation, Product.DataFamilySourceFileName)))
			{
				File.Copy(Path.Combine(Product.DataLocation, Product.DataFamilySourceFileName), Path.Combine(Product.UserDataLocation, Product.DataFamilySourceFileName));
			}
			if (!File.Exists(Path.Combine(Product.UserDataLocation, Product.DataSourceFileName)))
			{
				File.Copy(Path.Combine(Product.DataLocation, Product.DataSourceFileName), Path.Combine(Product.UserDataLocation, Product.DataSourceFileName));
			}
			if (!File.Exists(Path.Combine(Product.UserDataLocation, Product.SharedParametersFileName)))
			{
				File.Copy(Path.Combine(Product.DataLocation, Product.SharedParametersFileName), Path.Combine(Product.UserDataLocation, Product.SharedParametersFileName));
			}
			File.Copy(Path.Combine(Product.DataLocation, Product.PipeStandardFileName), Product.UserPipeStandardFile, true);
			Product.CreateUserConfigFile();
			return true;
		}

		public static void CreateUserConfigFile()
		{
			if (!File.Exists(Product.UserConfigFile))
			{
				new XElement("HYUserConfig").Save(Product.UserConfigFile);
			}
		}

		public static string GetSoftWareName()
		{
			return IniHelper.GetSetting("Product", "Name", Product.DefaultConfigFile, "盈建科机电");
		}

		public static string GetSoftWareVer()
		{
			return IniHelper.GetSetting("Product", "Version", Product.DefaultConfigFile, "2.0");
		}

		public static string GetSoftWareBuildVer()
		{
			return IniHelper.GetSetting("Product", "BuildVersion", Product.DefaultConfigFile, "2.0.0.0");
		}

		public static void SaveEncryptType(int type)
		{
			Product.SaveEncryptType(type);
		}

		public static int GetSoftWareType()
		{
			return IniHelper.GetIntSetting("Product", "Type", Product.ConfigFile, 1);
		}

		public static void SaveSoftWareType(int type)
		{
			IniHelper.WriteSetting("Product", "Type", type, Product.ConfigFile);
		}

		public static bool IsArchitecture()
		{
			int intSetting = IniHelper.GetIntSetting("Product", "DisciplineType", Product.DefaultConfigFile, -1);
			if (1 == intSetting)
			{
				return true;
			}
			string text = Product.GetSoftWareName().ToUpper();
			return text.Contains("ARCH") || text.Contains("乐建");
		}

		public static bool IsFamliyLeader()
		{
			int intSetting = IniHelper.GetIntSetting("Product", "DisciplineType", Product.DefaultConfigFile, -1);
			if (5 == intSetting)
			{
				return true;
			}
			string text = Product.GetSoftWareName().ToUpper();
			return text.Contains("FAMILY") || text.Contains("族") || text.Contains("构件") || text.Contains("库");
		}

		public static bool IsEDD()
		{
			int intSetting = IniHelper.GetIntSetting("Product", "DisciplineType", Product.DefaultConfigFile, -1);
			if (6 == intSetting)
			{
				return true;
			}
			string text = Product.GetSoftWareName().ToUpper();
			return text.Contains("EDD") || text.Contains("深") || text.Contains("综合") || text.Contains("支吊架");
		}

		public static bool IsDec()
		{
			int intSetting = IniHelper.GetIntSetting("Product", "DisciplineType", Product.DefaultConfigFile, -1);
			if (7 == intSetting)
			{
				return true;
			}
			string text = Product.GetSoftWareName().ToUpper();
			return text.Contains("DEC") || text.Contains("装饰") || text.Contains("装修");
		}

		public static bool IsMEI()
		{
			int intSetting = IniHelper.GetIntSetting("Product", "DisciplineType", Product.DefaultConfigFile, -1);
			if (20 == intSetting)
			{
				return true;
			}
			string text = Product.GetSoftWareName().ToUpper();
			return text.Contains("MEI") || text.Contains("安装");
		}

		public static bool IsRME()
		{
			int disciplineType = Product.GetDisciplineType();
			if (Product.HasDiscipline(disciplineType, 2) || Product.HasDiscipline(disciplineType, 3) || Product.HasDiscipline(disciplineType, 4))
			{
				return true;
			}
			string text = Product.GetSoftWareName().ToUpper();
			return (text.Contains("RME") || text.Contains("盈建科机电")) && !text.Contains("深化");
		}

		public static bool IsPC()
		{
			int disciplineType = Product.GetDisciplineType();
			if (8 == disciplineType)
			{
				return true;
			}
			string text = Product.GetSoftWareName().ToUpper();
			return text.Contains("PC") || text.Contains("装配式");
		}

		public static bool IsTunnel()
		{
			int disciplineType = Product.GetDisciplineType();
			if (9 == disciplineType)
			{
				return true;
			}
			string text = Product.GetSoftWareName().ToUpper();
			return text.Contains("Tunnel") || text.Contains("综合管廊");
		}

		private static int GetDisciplineType()
		{
			int num = IniHelper.GetIntSetting("Product", "DisciplineType", Product.DefaultConfigFile, 2);
			if (num < 0 || num > 31)
			{
				num = 2;
			}
			return num;
		}

		private static bool HasDiscipline(int type, int dt)
		{
			int num = dt - 1;
			return num >= 0 && 1 == (type >> num & 1);
		}

		public static DateTime GetLimitTime()
		{
			return new DateTime(2014, 9, 30);
		}

		public static void SaveTrialExpirationType(int daysOrTimes = 0)
		{
			IniHelper.WriteSetting("Product", "ExpirationType", daysOrTimes, Product.ConfigFile);
		}

		public static int GetTrialExpirationType()
		{
			return IniHelper.GetIntSetting("Product", "ExpirationType", Product.ConfigFile, 0);
		}

		public static void SaveTrialExecutionDays(int days)
		{
			IniHelper.WriteSetting("Product", "ExecutionDays", days, Product.ConfigFile);
		}

		public static int GetTrialExecutionDays()
		{
			return IniHelper.GetIntSetting("Product", "ExecutionDays", Product.ConfigFile, 0);
		}

		public static void SaveTrialExecutionCounter(int executionCounter)
		{
			IniHelper.WriteSetting("Product", "ExecutionCounter", executionCounter, Product.ConfigFile);
		}

		public static int GetTrialExecutionCounter()
		{
			int num = IniHelper.GetIntSetting("Product", "ExecutionCounter", Product.ConfigFile, 0);
			if (num < 0)
			{
				num = 0;
			}
			return num;
		}

		public static string GetContactServer()
		{
			return IniHelper.GetSetting("Product", "ContactServer", Product.ConfigFile, "192.168.0.1");
		}

		public static void SaveContactServer(string contactServer)
		{
			IniHelper.WriteSetting("Product", "ContactServer", contactServer, Product.ConfigFile);
		}

		public static int GetLockType()
		{
			int result = 1;
			int.TryParse(IniHelper.GetSetting("Product", "LockType", Product.ConfigFile, "1"), out result);
			return result;
		}

		public static void SaveLockType(int lt)
		{
			IniHelper.WriteSetting("Product", "LockType", lt, Product.ConfigFile);
		}

		public static string GetAuthorizationURL()
		{
			string valueDef = "http://www.hongye.com.cn/Applications/index.asp";
			return IniHelper.GetSetting("Product", "AuthorizationURL", Product.DefaultConfigFile, valueDef);
		}

		public static string GetEmailURL()
		{
			string valueDef = "MailTo:AEC@hongye.com.cn";
			return IniHelper.GetSetting("Product", "Email", Product.DefaultConfigFile, valueDef);
		}

		public static string GetHomePageURL()
		{
			string valueDef = "http://www.hongye.com.cn";
			return IniHelper.GetSetting("Product", "HomePage", Product.DefaultConfigFile, valueDef);
		}

		public static string GetForumURL()
		{
			string valueDef = "http://www.hongye.com.cn//forum";
			return IniHelper.GetSetting("Product", "Forum", Product.DefaultConfigFile, valueDef);
		}

		public static string[] GetTemplateName()
		{
			string setting = IniHelper.GetSetting("System", "TemplateName", Product.DefaultConfigFile, "HYBIMSpace给排水样板");
			char[] separator = new char[]
			{
				','
			};
			return setting.Split(separator, StringSplitOptions.RemoveEmptyEntries);
		}

		public static string[] GetTemplateFileName()
		{
			string setting = IniHelper.GetSetting("System", "TemplateFile", Product.DefaultConfigFile, "HYBIMSpace给排水样板.rte");
			char[] separator = new char[]
			{
				','
			};
			return setting.Split(separator, StringSplitOptions.RemoveEmptyEntries);
		}

		public static int GetDisciplineValue()
		{
			if (Product.IsArchitecture() || Product.IsFamliyLeader() || Product.IsEDD() || Product.IsMEI() || Product.IsPC() || Product.IsTunnel())
			{
				return 1;
			}
			string setting = IniHelper.GetSetting("Product", "DisciplineType", Product.ConfigFile, "1");
			int num = 2;
			int.TryParse(setting, out num);
			if (num < 2 || num > Product.maxDisciplineValue)
			{
				num = 2;
			}
			return num;
		}

		public static void SaveDisciplineValue(int disciplineValue)
		{
			if (disciplineValue < 1 || disciplineValue > Product.maxDisciplineValue)
			{
				disciplineValue = 2;
			}
			IniHelper.WriteSetting("Product", "DisciplineType", disciplineValue, Product.ConfigFile);
		}

		public static bool HasDiscipline(int type, Product.DisciplineType dt)
		{
			int num = dt - Product.DisciplineType.Architecture;
			return num >= 0 && 1 == (type >> num & 1);
		}

		public static int SetDiscipline(ref int type, Product.DisciplineType dt, bool flag)
		{
			int num = dt - Product.DisciplineType.Architecture;
			if (num < 0)
			{
				return type;
			}
			if (num < 0)
			{
				return type;
			}
			if (flag)
			{
				type |= 1 << num;
			}
			else
			{
				type &= ~(1 << num);
			}
			return type;
		}

		public static Product.EncryptType ToEncryptType(int type)
		{
			if (type < 1 || type > 4)
			{
				type = 1;
			}
			return (Product.EncryptType)type;
		}

		public static Product.EncryptType GetEncryptType()
		{
			string setting = IniHelper.GetSetting("Product", "Type", Product.ConfigFile, "1");
			int type = 1;
			int.TryParse(setting, out type);
			return Product.ToEncryptType(type);
		}

		public static void SaveEncryptType(Product.EncryptType et)
		{
			int num = (int)et;
			IniHelper.WriteSetting("Product", "Type", num.ToString(), Product.ConfigFile);
		}

		public static string[] GetSourceAddInFile(bool beAddZero = false)
		{
			string setting = IniHelper.GetSetting("Addin", "SOURCE_FILENAME", Product.DefaultConfigFile, "YArchitech.addin");
			char[] separator = new char[]
			{
				','
			};
			string[] array = setting.Split(separator, StringSplitOptions.RemoveEmptyEntries);
			string[] array2 = new string[array.Count<string>()];
			for (int i = 0; i < array.Count<string>(); i++)
			{
				string text = array[i].Trim();
				if (!text.EndsWith(".addin", StringComparison.OrdinalIgnoreCase))
				{
					text += ".addin";
				}
				if (beAddZero)
				{
					array2[i] = text.Insert(0, "00");
				}
				else
				{
					array2[i] = text;
				}
			}
			return array2;
		}

		public static string[] GetYJKArchSourceAddInFile()
		{
			string setting = IniHelper.GetSetting("Addin", "HYARCH_SOURCE_FILENAME", Product.DefaultConfigFile, "YArchitech.addin");
			char[] separator = new char[]
			{
				','
			};
			return setting.Split(separator, StringSplitOptions.RemoveEmptyEntries);
		}

		public static string[] GetHYRMESourceAddInFile()
		{
			string setting = IniHelper.GetSetting("Addin", "HYRME_SOURCE_FILENAME", Product.DefaultConfigFile, "YArchitech.addin");
			char[] separator = new char[]
			{
				','
			};
			return setting.Split(separator, StringSplitOptions.RemoveEmptyEntries);
		}

		public static bool BeStartingAtTheSameTime()
		{
			bool result = false;
			if (IniHelper.GetSetting("StartUp", "StartSameTime", Product.ConfigFile, "1").Equals("1"))
			{
				result = true;
			}
			return result;
		}

		public static void SaveStartingAtTheSameTime(bool beSame)
		{
			string value = "0";
			if (beSame)
			{
				value = "1";
			}
			IniHelper.WriteSetting("StartUp", "StartSameTime", value, Product.ConfigFile);
		}

		public static bool BeJustStarting()
		{
			bool result = false;
			if (IniHelper.GetSetting("StartUp", "StartJust", Product.ConfigFile, "0").Equals("1"))
			{
				result = true;
			}
			return result;
		}

		public static void SaveJustStarting(bool beSame)
		{
			string value = "0";
			if (beSame)
			{
				value = "1";
			}
			IniHelper.WriteSetting("StartUp", "StartJust", value, Product.ConfigFile);
		}

		public static string GetPlatformAddInFolder()
		{
			return IniHelper.GetSetting("StartUp", "AddInFolder", Product.ConfigFile, "");
		}

		public static List<string> GetPlatformAllAddInFolders()
		{
			return Product.GetSpecPaths(Product.GetPlatformAddInFolder());
		}

		private static List<string> GetSpecPaths(string basePath)
		{
			List<string> list = new List<string>();
			if (string.IsNullOrEmpty(basePath))
			{
				return list;
			}
			int num = basePath.LastIndexOf('\\');
			if (num < 1)
			{
				return list;
			}
			string arg = basePath.Substring(0, num + 1);
			for (int i = 4; i < 9; i++)
			{
				list.Add(arg + "201" + i);
			}
			return list;
		}

		public static void SaveRevitAddInFolderPath(string folderPath)
		{
			IniHelper.WriteSetting("StartUp", "AddInFolder", Product.ConfigFile, folderPath);
		}

		public static void SaveUserAddInFolderPath(string folderPath)
		{
			IniHelper.WriteSetting("StartUp", "UserAddInFolder", Product.ConfigFile, folderPath);
		}

		public static string GetUserAddInFolder()
		{
			return IniHelper.GetSetting("StartUp", "UserAddInFolder", Product.ConfigFile, "");
		}

		public static bool CopySpecAddInFile()
		{
			bool result;
			try
			{
				foreach (string text in Product.GetPlatformAllAddInFolders())
				{
					Directory.CreateDirectory(text);
					string[] sourceAddInFile = Product.GetSourceAddInFile(false);
					for (int i = 0; i < sourceAddInFile.Length; i++)
					{
						string text2 = sourceAddInFile[i].Trim();
						if (!text2.EndsWith(".addin", StringComparison.OrdinalIgnoreCase))
						{
							text2 += ".addin";
						}
						string text3 = Path.Combine(Product.DataLocation, text2);
						if (File.Exists(text3))
						{
							string fileName = Path.Combine(text, text2.Insert(0, "00"));
							string path = Product.BinLocation;
							XElement xelement = XElement.Load(text3);
							XElement xelement2 = xelement.Element("AddIn").Element("Assembly");
							string value = xelement2.Value;
							xelement2.Value = Path.Combine(path, value);
							xelement.Save(fileName);
						}
					}
				}
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public static List<string> GetAddinFileList(string path, bool bl)
		{
			List<string> list = new List<string>();
			if (!Directory.Exists(path))
			{
				return list;
			}
			foreach (FileInfo fileInfo in new DirectoryInfo(path).GetFiles())
			{
				if (fileInfo.Extension.Equals(".addin"))
				{
					string fileName = Path.GetFileName(fileInfo.Name);
					list.Add(fileName);
				}
			}
			return list;
		}

		public static List<string> GetOrDelAddinFile(string path, List<string> lstAddin, bool blDel)
		{
			List<string> addinFileList = Product.GetAddinFileList(path, true);
			if (addinFileList.Count == 0)
			{
				return null;
			}
			List<string> collection = addinFileList.Intersect(lstAddin).ToList<string>();
			List<string> collection2 = addinFileList.Union(lstAddin).ToList<string>().Except(lstAddin).ToList<string>();
			List<string> list = new List<string>();
			if (blDel)
			{
				list.AddRange(collection);
			}
			else
			{
				list.AddRange(collection2);
			}
			foreach (string path2 in list)
			{
				string path3 = Path.Combine(path, path2);
				try
				{
					if (File.Exists(path3))
					{
						File.Delete(path3);
					}
				}
				catch
				{
				}
			}
			return list;
		}

		public static bool DelAddinFile(string path)
		{
			List<string> specPaths = Product.GetSpecPaths(path);
			if (specPaths == null || specPaths.Count < 1)
			{
				return false;
			}
			foreach (string text in specPaths)
			{
				List<string> addinFileList = Product.GetAddinFileList(text, true);
				if (addinFileList.Count == 0)
				{
					return false;
				}
				foreach (string path2 in addinFileList)
				{
					string path3 = Path.Combine(text, path2);
					try
					{
						if (File.Exists(path3))
						{
							File.Delete(path3);
						}
					}
					catch
					{
					}
				}
			}
			return true;
		}

		private static string installLocation;

		private static string binLocation = string.Empty;

		private static readonly string defaultConfigFile = Path.Combine(Product.InstallLocation, "HYRME.ini");

		private static readonly string configFile = Path.Combine(Product.WorkingLocation, "HYRME.ini");

		private static string programDataLocation = string.Empty;

		private static string workingLocation = string.Empty;

		private static string userConfigFileName = null;

		private static string m_userConfigFile;

		private static string m_DataSourceFilename = string.Empty;

		private static string m_DataFamilySourceFilename = string.Empty;

		private static string m_userDataSourceFile;

		private static string m_PipeStandardFileName = string.Empty;

		private static string m_userSharedParametersFile;

		private const string m_ProductSection = "Product";

		public static readonly int disciplineCount = Enum.GetValues(typeof(Product.DisciplineType)).Length;

		public static readonly int maxDisciplineValue = (1 << Product.disciplineCount) - 1;

		public enum DisciplineType
		{
			Architecture = 1,
			HVAC,
			Drainage,
			Electric,
			Family,
			EDD,
			MEI,
			PC,
			Tunnel
		}

		public enum EncryptType
		{
			Trial = 1,
			Standalone,
			Network,
			Education
		}
	}
}
